package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.AddCourseDTO;
import com.woniu.pojo.Course;
import com.woniu.pojo.Trainer;
import com.woniu.service.ICourseService;
import com.woniu.service.ITrainerService;
import com.woniu.util.PageData;
import com.woniu.util.Result;
import com.woniu.vo.CoursePublishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiaoruan
 * @since 2023-03-15
 */

@Api(tags = "课程管理")
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {
    @Autowired
    ICourseService courseService;

    @Autowired
    ITrainerService trainerService;

    @ApiOperation("课程添加")
    @PostMapping
    public Result addCourse(@RequestBody AddCourseDTO addCourseDTO) {

        String courseId = courseService.addcourse(addCourseDTO);
        return toDataResult(courseId);

    }

    @ApiOperation("根据id查询课程信息")
    @GetMapping("find/{courseId}")
    public Result findById(@PathVariable String courseId) {
        AddCourseDTO addCourseDTO =courseService.selectById(courseId);
        return toDataResult(addCourseDTO);
    }

    @ApiOperation("发布课程")
    @PutMapping("update/{courseId}")
    public Result updateStatus(@PathVariable String courseId) {

        courseService.updateStatus(courseId);

        return success();
    }

    @ApiOperation("根据courseId查询课程发布信息")
    @GetMapping("getPublishVO/{courseId}")
    public Result findPublishVo(@PathVariable String courseId) {

        CoursePublishVO coursePublishVo =courseService.selectPublishVO(courseId);

        return toDataResult(coursePublishVo);
    }

    @ApiOperation("分页查询课程发布列表")
    @GetMapping("getpageList/{currentPage}/{pageSize}")
    public Result getpageList(@PathVariable Integer currentPage,@PathVariable Integer pageSize) {
        Page<Course> p = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getStatus,"Normal");

        Page<Course> pageList = courseService.page(p,queryWrapper);
        long total = pageList.getTotal();
        List<Course> records = pageList.getRecords();
        records.stream().forEach(e->{
            Trainer trainer = trainerService.getById(e.getTrainerId());
            e.setTrainer(trainer);
        });


        PageData pageData = new PageData();
        pageData.setTotal(total);
        pageData.setData(records);

        return toDataResult(pageData);
    }




}
